package com.jiazhong.example.project.shoppingcart.controller;

import com.alibaba.fastjson.JSONArray;
import com.jiazhong.example.project.shoppingcart.bean.Users;
import com.jiazhong.example.project.shoppingcart.service.UsersService;
import com.jiazhong.example.project.shoppingcart.service.impl.UsersServiceImpl;
import com.jiazhong.example.project.shoppingcart.util.JsonResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/users")
public class UsersController extends BaseController {

    private UsersService usersService = new UsersServiceImpl();

    public void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String phone = request.getParameter("phone");
        Users user = new Users();
        user.setUsername(username);
        user.setPassword(password);
        user.setPhone(phone);
        JsonResult result = usersService.save(user);
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println(JSONArray.toJSONString(result));
    }


    public void check_username(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String username = request.getParameter("username");
        JsonResult result = usersService.checkUsername(username);
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println(JSONArray.toJSONString(result));
    }

    public void check_phone(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String phone = request.getParameter("phone");
        JsonResult result = usersService.checkPhone(phone);
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println(JSONArray.toJSONString(result));
    }

    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        // 获取到输入的验证码
        String yzm_code = request.getParameter("yzm_code");
        // 获取到系统保存的验证码
        Object yzmCode = request.getSession().getAttribute("yzm_code");

        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        // 校验验证码是否正确
        if (!yzm_code.equals(yzmCode.toString())) {
            out.println(JSONArray.toJSONString(new JsonResult<>(500, "验证码错误")));
            return;
        }
        // 验证码正确后
        JsonResult login = usersService.login(username, password, request.getSession());
        out.println(JSONArray.toJSONString(login));
    }

    public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().removeAttribute("user");
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println(JSONArray.toJSONString(new JsonResult<>("success")));

    }
}
